/*
 * Copyright 2011 Niklas Kyster Rasmussen
 * 
 * This file is part of WotXP.
 *
 * WotXP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * WotXP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WotXP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package net.nikr.wot.calc;


public class Calc {
	
	final static double CONSTANT =  Math.pow(100.0, (1.0/100.0));

	public static int xpDone(int toLevel, Traning traning){
		int total = 0;
		switch (traning){
			case SECONDARY3:
				total = xpDone(0, total, toLevel, Traning.SECONDARY3);
				toLevel = 100;
			case SECONDARY2:
				total = xpDone(0, total, toLevel, Traning.SECONDARY2);
				toLevel = 100;
			case SECONDARY1:
				total = xpDone(0, total, toLevel, Traning.SECONDARY1);
				toLevel = 100;
			case PRIMARY:
				total = xpDone(50, total, toLevel, Traning.PRIMARY);
				toLevel = 100;
		}
		return total;
	}
	
	private static int xpDone(int start, int total, int level, Traning traning){
		for (int a = start; a < level; a++){ //50 - 
			total += Calc.xpToNextLevel(a, traning);
		}
		return total;
	}
	
	public static int xpLeft(int level, Traning traning){
		return xpTo(level, 100, traning);
	}
	
	public static int xpTo(int start, int end, Traning traning){
		if (start < 0) start = 0;
		if (end < 1) end = 1;
		if (start > 100) start = 100;
		if (end > 100) end = 100;
		if (end < start) end = start;
		
		int total = 0;
		for (int a = start; a < end; a++){ //50 - 
			total += Calc.xpToNextLevel(a, traning);
		}
		return total;
	}
	
	public static int xpToNextLevel(int level, Traning traning){
		//XXX - Rounding unknown: floor or round (+/- 14xp) (NOT Ceil!)
		return (int)Math.floor(traning.get()*Math.pow(CONSTANT, level));
	}
	
	public static int battlesLeft(double xpLeft, double avg){
		return (int)Math.ceil(xpLeft / avg);
	}
	
	public static int battlesLeftRapid(double xpLeft, double avg, double crew){
		return (int)Math.ceil(xpLeft / (avg+(avg/crew)));
	}
}
